﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using DblogStudio.BLL;

namespace DblogStudio.Utils
{
    class ConfigUtil
    {
        public static List<XElement> GetConfig(string elementName)
        {
            try
            {
                var xelement = XElement.Load("Config.xml");
                if (xelement != null)
                {
                    var name = (from nm in xelement.Elements(elementName)
                                select nm).ToList();
                    return name;
                }
                else
                    return null;
            }
            catch
            {
                throw new Exception("Không thể tải file Config.XML");
            }
        }

        public static CODE_RESULT_RETURN Update(string elementName, string key, string value)
        {
            try
            {
                var xelement = XElement.Load("Config.xml");
                if (xelement != null)
                {
                    if (key.IsNullOrEmpty())
                    {
                        var name = (from nm in xelement.Elements(elementName)
                                    select nm).FirstOrDefault();
                        if (name != null)
                        {
                            name.Value = value.Trim();
                            xelement.Save("Config.xml");
                            return CODE_RESULT_RETURN.ThanhCong;
                        }
                        else
                            return CODE_RESULT_RETURN.ThatBai;
                    }
                    else
                    {
                        var name = (from nm in xelement.Elements(elementName)
                                    where nm.Attribute("id").Value == key
                                    select nm).FirstOrDefault();
                        if (name != null)
                        {
                            name.Value = value.Trim();
                            xelement.Save("Config.xml");
                            return CODE_RESULT_RETURN.ThanhCong;
                        }
                        else
                            return CODE_RESULT_RETURN.ThatBai;
                    }
                }
                else
                    return CODE_RESULT_RETURN.ThatBai;
            }
            catch
            {
                throw new Exception("Không thể tải file Config.XML");
            }
        }

        public static CODE_RESULT_RETURN CreateConfigFile()
        {
            try
            {
                string xml = @"<?xml version='1.0' encoding='utf-8' ?>
                                <data>
                                  <nguoiquanly id='truongphong'>TS. Lê Thị Kim Lan</nguoiquanly>
                                  <nguoiquanly id='phophong'>TS. Hoàng Đình Trung</nguoiquanly>
                                  <timetowaitexport>3000</timetowaitexport>
                                </data>
                                ";
                var xelement = XElement.Parse(xml);
                xelement.Save("Config.xml");
                return CODE_RESULT_RETURN.ThanhCong;
            }
            catch (Exception ex)
            {
                throw new Exception("Lỗi lưu file config. Chi tiết: " + ex.Message);
            }
        }
    }
}
